//
//  ContentView.swift
//  BookNew
//
//  Created by iMac on 2021/4/7.
//  Copyright © 2021 iMac. All rights reserved.
//

import SwiftUI

struct ContentView: View {
    @State var text = ""
    @StateObject var topModel = TopToolView.ToolModel()
    @StateObject var webModel = WebView.WebModel()
    
    
    var body: some View {
        VStack(content: {
            TopToolView(model: topModel)
            //            Text("URL: \(topModel.urlString)")
            
//            if topModel.isRead {
//
//                TextView(text: $webModel.contentText, goEnd: {
//                    webModel.nextAction()
//                })
//                .padding(10)
//                .onTapGesture {
//                    if(!topModel.isShow){
//                        topModel.isShow = true
//                    }
//                }
//
//            } else {
//                WebView(model: webModel, urlString: $topModel.urlString, isRead: $topModel.isRead)
//            }
            
            ZStack {
                WebView(model: webModel, urlString: $topModel.urlString, isRead: $topModel.isRead)
                if topModel.isRead {
                    
                    TextView(text: $webModel.contentText, goEnd: {
                        webModel.nextAction()
                    })
                    .padding(10)
                    .onTapGesture {
                        if(!topModel.isShow){
                            topModel.isShow = true
                        }
                    }
                    
                }
            }
            
            //                        Text("Bottom").padding(30).onTapGesture {
            //                            let t = self.text + "123123\n"
            //                            self.text = t
            //                        }
        }).animation(.easeInOut(duration: 0.3))
        
    }
}

// 包装UITextView
struct TextView: UIViewRepresentable {
    @Binding var text: String
    
    var goEnd: (()->Void)?
    
    func makeUIView(context: UIViewRepresentableContext<Self>) -> UITextView {
        let view = UITextView()
        view.isEditable = false
        view.font = UIFont.systemFont(ofSize: 24)
        view.delegate = context.coordinator
        return view
    }
    
    func updateUIView(_ uiView: UITextView, context: UIViewRepresentableContext<Self>) {
        print("TextView updateUIView")
        if context.coordinator.lastText != text {
            uiView.text = text
            context.coordinator.lastText = text
            context.coordinator.requestLimit = true
        }
    }
    
    func makeCoordinator() -> Coordinator {
        print("TextView makeCoordinator")
        return Coordinator($text, goEnd)
    }
    
    class Coordinator: NSObject, UITextViewDelegate {
        var requestLimit = false

        var text: Binding<String>
        var goEnd: (()->Void)?
        var lastText: String?
        
        init(_ text: Binding<String>,_ goEnd: (()->Void)?) {
            self.text = text
            self.goEnd = goEnd
        }
        
        func textViewDidChange(_ textView: UITextView) {
            self.text.wrappedValue = textView.text
        }
        
        func scrollViewDidScroll(_ scrollView: UIScrollView) {
            let yP = scrollView.contentOffset.y
            let maxY = scrollView.contentSize.height
            print("yP \(yP), maxY \(maxY)")
            if yP > 100 && maxY - yP < 1600 && requestLimit {
                print("requestLimit false")
                requestLimit = false
                // 请求下一页
                if goEnd != nil {
                    goEnd!()
                }
            }
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        Group {
            ContentView()
        }
    }
}
